package keer.httpinterface.http;

import keer.httpinterface.utils.AnnotationInfo;
import keer.httpinterface.utils.CacheMap;

import java.lang.reflect.Method;
import java.util.Objects;

public class DefaultHttpRequestContextFactory implements HttpRequestContextFactory {
    private final static CacheMap<CacheKey, HttpRequestContext> CONTEXT_CACHE = new CacheMap<>();
    private static final String KEY_URL = "url";
    private static final String KEY_HEADERS = "headers";
    private static final String KEY_PARAMS = "params";
    private static final String KEY_METHOD = "method";
    private static final String KEY_CHARSET = "charset";
    private static final String KEY_CONTENT_TYPE = "contentType";
    //    private static final String KEY_CONTENT_TYPE_STRING = "contentTypeString";
    private static final String KEY_INTERCEPTORS = "interceptors";
    private HttpParamsResolver paramsResolver;

    protected DefaultHttpRequestContextFactory() {
    }

    @Override
    public HttpRequestContext create(Class<?> interfaceClass, Method interfaceMethod) {
        return CONTEXT_CACHE.loadOrCreate(new CacheKey(interfaceClass, interfaceMethod), () -> {
            Context context = new Context();
            initContext(context, interfaceClass, interfaceMethod);
            return context;
        });
    }

    private static void initContext(Context context, Class<?> interfaceClass, Method interfaceMethod) {

    }

    private static class CacheKey {
        private final Class<?> interfaceClass;
        private final Method interfaceMethod;
        private CacheKey(Class<?> interfaceClass,
                         Method interfaceMethod) {
            this.interfaceClass = interfaceClass;
            this.interfaceMethod = interfaceMethod;
        }

        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof CacheKey)) {
                return false;
            }
            CacheKey c = (CacheKey) obj;
            return interfaceClass == c.interfaceClass &&
                    interfaceMethod == c.interfaceMethod;
        }

        @Override
        public int hashCode() {
            return Objects.hash(interfaceClass, interfaceMethod);
        }
    }

    private class Context implements HttpRequestContext {
        private Headers headers;
        private HttpParams params;
        private String url;
        private HttpMethod method;
        private Class<?> interfaceClass;
        private Method interfaceMethod;
        private AnnotationInfo httpInterfaceAnnotation;
        private AnnotationInfo requestAnnotation;

        @Override
        public String url() {
            return url;
        }

        @Override
        public Headers headers() {
            return headers;
        }

        @Override
        public HttpMethod method() {
            return method;
        }

        @Override
        public Class<?> interfaceClass() {
            return interfaceClass;
        }

        @Override
        public Method interfaceMethod() {
            return interfaceMethod;
        }

        @Override
        public AnnotationInfo httpInterfaceAnnotation() {
            return httpInterfaceAnnotation;
        }

        @Override
        public AnnotationInfo requestAnnotation() {
            return requestAnnotation;
        }
    }
}
